// by Xeno
//#define __DEBUG__
#define THIS_FILE "x_setupplayer.sqf"
#include "x_setup.sqf"
private ["_p", "_pos", "_type", "_weapp", "_magp", "_res", "_taskstr", "_color", "_counterxx", "_text", "_mcol", "_s", "_trigger", "_types", "_action", "_ar", "_tactionar", "_primw", "_muzzles"];

diag_log [diag_frameno, diag_ticktime, time, "Executing Dom x_setupplayer.sqf"];

GVAR(still_in_intro) = true;
GVAR(sqtmgmtblocked) = false;

#ifndef __TT__
if (GVAR(EnableBaseSafeZone) == 0) then {
	_shield = GVAR(ProtectionZone_inv) createVehicleLocal (position GVAR(FLAG_BASE));
	_shield setDir -211;
	_shield setPos (position GVAR(FLAG_BASE));
	if (GVAR(ShowBaseSafeZone) == 0) then {
		_shield = GVAR(ProtectionZone) createVehicleLocal (position GVAR(FLAG_BASE));
		_shield setDir -211;
		_shield setPos [getPosASL GVAR(FLAG_BASE) select 0, getPosASL GVAR(FLAG_BASE) select 1, -28.45];
		_shield setObjectTexture [0,"#(argb,8,8,3)color(0,0,0,0.7,ca)"];
	};
};
#endif

GVAR(current_defend_target) = "";
GVAR(current_defend_idx) = -1;
GVAR(current_attack_target) = "";
GVAR(current_attack_idx) = -1;

__ccppfln(x_client\x_f\x_perframe.sqf);

GVAR(name_pl) = name player;
GVAR(player_faction) = faction player;

__pSetVar ["BIS_noCoreConversations", true];

_p = player;
__pSetVar [QGVAR(alivetimestart), time];
_pos = position _p;
_type = typeOf _p;
GVAR(string_player) = str(player);
GVAR(player_side) = playerSide;
GVAR(player_str_group) = str(group player);
if (GVAR(player_str_group) != "") then {
	_ar = toArray GVAR(player_str_group);
	_ar set [0,-99]; _ar set [1,-99];
	_ar = _ar - [-99];
	GVAR(player_str_group) = toString _ar;
};
// no idea if the following really works and it should never happen!
if (isNull (group player)) then {
	// TODO: What are the factions in A3?
	_gside = if ((faction player) in ["USMC","CDF","BIS_US","BIS_CZ","BIS_GER","BIS_BAF","BLU_F","BLU_G_F"]) then {blufor} else {opfor};
	_grpp = createGroup blufor;
	[player] joinSilent _grpp;
};
if (side (group player) != GVAR(player_side)) then {
	GVAR(player_side) = side (group player);
};

__pSetVar [QGVAR(tk_cutofft), -1];
__pSetVar ["xr_pluncon", false,true];
if (GVAR(WithRevive) == 1) then {
	__pSetVar [QGVAR(last_gear_save), -1];
	player addEventHandler ["handleDamage", {_this call FUNC(playerHD)}];
	/*0 spawn {
		_timeend = time + 20;
		waitUntil {!isNil "BIS_blendColorAlpha" || {time > _timeend}};
		sleep 0.01;
		__TRACE("adding ClientHD EH");
		player removeAllEventHandlers "handleDamage";
		player addEventHandler ["handleDamage", {_this call FUNC(playerHD)}];
	};*/
};

if (GVAR(with_ranked)) then {GVAR(sm_p_pos) = nil};

#ifdef __TT__
GVAR(own_side) = if (GVAR(player_side) == opfor) then {"OPFOR"} else {"BLUFOR"};
GVAR(side_player_str) = switch (GVAR(player_side)) do {
	case blufor: {"blufor"};
	case opfor: {"opfor"};
};
GVAR(own_side_trigger) = if (GVAR(player_side) == opfor) then {"EAST"} else {"WEST"};
GVAR(own_side_trigger_alt) = if (GVAR(player_side) == opfor) then {"OPFOR"} else {"BLUFOR"};

GVAR(side_player) = GVAR(player_side);

GVAR(rep_truck) = if (GVAR(own_side) == "BLUFOR") then {GVAR(rep_truck_blufor)} else {GVAR(rep_truck_opfor)};

GVAR(create_bike) = if (GVAR(own_side) == "OPFOR") then {["O_Quadbike_01_F"]} else {["B_G_Quadbike_01_F"]};

if (GVAR(with_mgnest)) then {
	GVAR(mg_nest) = if (GVAR(own_side) == "OPFOR") then {"WarfareBMGNest_PK_TK_EP1"} else {"WarfareBMGNest_M240_US_EP1"};
};
#endif

#ifndef __TT__
GVAR(name_flag_base) = GVAR(FLAG_BASE);
#else
GVAR(name_flag_base) = switch (GVAR(player_side)) do {
	case blufor: {GVAR(WFLAG_BASE)};
	case opfor: {GVAR(EFLAG_BASE)};
};
#endif

GVAR(the_box) = switch (GVAR(own_side)) do {
	case "INDEPENDENT": {"Box_NATO_Wps_F"};
	case "OPFOR": {"Box_East_Wps_F"};
	case "BLUFOR": {"Box_NATO_Wps_F"};
};
GVAR(the_base_box) = switch (GVAR(own_side)) do {
	case "INDEPENDENT": {"Box_NATO_WpsSpecial_F"};
	case "OPFOR": {"Box_East_WpsSpecial_F"};
	case "BLUFOR": {"Box_NATO_WpsSpecial_F"};
};

GVAR(flag_vec) = objNull;

if (!isServer) then {execVM "x_bikb\kbinit.sqf"};

if (!X_SPE) then {
#ifndef __TT__
	GVAR(X_DropZone) = __XJIPGetVar(X_DropZone);
#endif
	GVAR(AriTarget) = __XJIPGetVar(GVAR(AriTarget));
	GVAR(AriTarget2) = __XJIPGetVar(GVAR(AriTarget2));
};

[QGVAR(dummy_marker), [0,0,0],"ICON","ColorBlack",[1,1],"",0,"Empty"] call FUNC(CreateMarkerLocal);
[QGVAR(arti_target2), [0,0,0],"ICON","ColorBlue",[1,1],(localize "STR_DOM_MISSIONSTRING_498"),0,"mil_destroy"] call FUNC(CreateMarkerLocal);
QGVAR(arti_target2) setMarkerPosLocal getPosASL GVAR(AriTarget2);
[QGVAR(arti_target), [0,0,0],"ICON","ColorBlue",[1,1],(localize "STR_DOM_MISSIONSTRING_499"),0,"mil_destroy"] call FUNC(CreateMarkerLocal);
QGVAR(arti_target) setMarkerPosLocal getPosASL GVAR(AriTarget);
#ifndef __TT__
[QGVAR(drop_zone), [0,0,0],"ICON","ColorBlue",[1,1],(localize "STR_DOM_MISSIONSTRING_500"),0,"mil_dot"] call FUNC(CreateMarkerLocal);
QGVAR(drop_zone) setMarkerPosLocal getPosASL GVAR(X_DropZone);
#endif

if (__XJIPGetVar(GVAR(the_end))) exitWith {
	endMission "END1";
	forceEnd;
};

GVAR(mob_respawns) = [];

#ifndef __TT__
	{
		if (_x select 1 > 99) exitWith {};
		GVAR(mob_respawns) set [count GVAR(mob_respawns), _x select 0];
	} forEach GVAR(p_vecs);
#else
	if (GVAR(player_side) == blufor) then {
		{
			if (_x select 1 > 99) exitWith {};
			GVAR(mob_respawns) set [count GVAR(mob_respawns), _x select 0];
		} forEach GVAR(p_vecs_blufor);
	} else {
		{
			if (_x select 1 > 1099) exitWith {};
			GVAR(mob_respawns) set [count GVAR(mob_respawns), _x select 0];
		} forEach GVAR(p_vecs_opfor);
	};
#endif

[QGVAR(the_end), {
	if (_this == 0) then {
		if (isNil QGVAR(end_cam_running)) then {
			execVM "x_client\x_endcam.sqf";
		};
	} else {
		endMission "END1";
		forceEnd;
	};
}] call FUNC(NetAddEventToClients);
[QGVAR(recaptured), {_this call FUNC(RecapturedUpdate)}] call FUNC(NetAddEventToClients);
[QGVAR(doarti), {if (alive player && {(player distance _this < 50)} && {!__pGetVar(xr_pluncon)}) then {(localize "STR_DOM_MISSIONSTRING_501") call FUNC(HQChat)}}] call FUNC(NetAddEventToClients);
[2, QGVAR(m_box), {_this call FUNC(create_boxNet)}] call FUNC(NetAddEvent);
[2, QGVAR(r_box), {
	private ["_nobjs", "_box", "_unit"];
	_nobjs = nearestObjects [_this select 0, [GVAR(the_box)], 10];
	__TRACE_2("r_box","_nobjs","_this");
	if (count _nobjs > 0) then {
		_box = _nobjs select 0;
		_unit = _this select 1;
		if (!isNil "_unit" && {!isNull _unit}) then {
			_unit setVariable [QGVAR(boxcargo), [getWeaponCargo _box, getMagazineCargo _box, getItemCargo _box]];
		};
		deleteVehicle _box;
	};
}] call FUNC(NetAddEvent);
// TYRGHEN - Going through the m_box
//[QGVAR(air_box), {_box = GVAR(the_box) createVehicleLocal _this;_box setPos [_this select 0,_this select 1,0];player reveal _box;[_box] call FUNC(weaponcargo);_box addEventHandler ["killed",{deleteVehicle (_this select 0)}]}] call FUNC(NetAddEventToClients);
[2, QGVAR(air_box), {_this call FUNC(create_boxNet)}] call FUNC(NetAddEvent);

[QGVAR(sm_res_client), {
	__TRACE_1("sm_res_client _this","_this");
	playSound "Notebook";
	GVAR(sm_winner) = _this select 0;
	if (GVAR(with_ranked)) then {
		GVAR(sm_running) = false
	};
	__TRACE_1("sm_res_client GVAR(sm_oldsm_idx)","d_sm_oldsm_idx");
	deleteMarkerLocal (format ["XMISSIONM%1", GVAR(sm_oldsm_idx) + 1]);
	if (GVAR(x_sm_type) == "convoy" || {GVAR(x_sm_type) == "deliver"}) then {deleteMarkerLocal (format ["XMISSIONM2%1", GVAR(sm_oldsm_idx) + 1])};
	(_this select 1) spawn FUNC(sidemissionwinner)
}] call FUNC(NetAddEventToClients);
[QGVAR(target_clear), {playSound "fanfare";_this spawn FUNC(target_clear_client)}] call FUNC(NetAddEventToClients);
[QGVAR(update_target), {0 spawn FUNC(createnexttargetclient)}] call FUNC(NetAddEventToClients);
[QGVAR(priority_target), {true spawn FUNC(createprioritytargetclient)}] call FUNC(NetAddEventToClients);
[QGVAR(priority_done), {
	private ["_pform","_ptask"];
	_pform = format["d_prio%1", (__XJIPGetVar(GVAR(current_target_index)) + 2)];
	_ptask = __getMNsVar2(_pform);
	if (_this) then {
		["TaskSucceeded",[_ptask, (taskDescription _ptask) select 1]] call BFUNC(showNotification);
		_ptask setTaskState "Succeeded";
	} else {
		["TaskCanceled",[_ptask, (taskDescription _ptask) select 1]] call BFUNC(showNotification);
		_ptask setTaskState "Canceled";
	};
	deleteMarker format["mtprio_%1",__XJIPGetVar(GVAR(current_target_index))];
}] call FUNC(NetAddEventToClients);
[QGVAR(up_m), {
	__TRACE("up_m getsidemissionclient");
	[true] spawn FUNC(getsidemissionclient)
}] call FUNC(NetAddEventToClients);
[QGVAR(unit_tk), {
	if (GVAR(sub_tk_points) != 0) then {
		[format [(localize "STR_DOM_MISSIONSTRING_502"), _this select 0, _this select 1, GVAR(sub_tk_points)], "GLOBAL"] call FUNC(HintChatMsg);
	} else {
		[format [(localize "STR_DOM_MISSIONSTRING_503"), _this select 0, _this select 1], "GLOBAL"] call FUNC(HintChatMsg);
	};
}] call FUNC(NetAddEventToClients);
[QGVAR(unit_tk2), {
	if (GVAR(sub_tk_points) != 0) then {
		[format [(localize "STR_DOM_MISSIONSTRING_504"), _this select 0, _this select 1, GVAR(sub_tk_points)], "GLOBAL"] call FUNC(HintChatMsg);
	} else {
		[format [(localize "STR_DOM_MISSIONSTRING_505"), _this select 0, _this select 1], "GLOBAL"] call FUNC(HintChatMsg);
	};
}] call FUNC(NetAddEventToClients);
[QGVAR(ataxi), {_this call FUNC(ataxiNet)}] call FUNC(NetAddEventSTO);
//[QGVAR(ai_kill), {if ((_this select 0) in (units (group player)) && {player == leader (group player)}) then {[QGVAR(pas), [player, _this select 1 ]] call FUNC(NetCallEventCTS)}}] call FUNC(NetAddEventToClients);
[QGVAR(p_ar), {
#ifdef __DEBUG__
	_uidp = getPlayerUID player;
	_suid = _this select 2;
	__TRACE_2("p_ar","_uidp","_suid");
#endif
	_this call FUNC(player_stuff);
	QGVAR(p_ar) call FUNC(NetRemoveEventSTO);
}] call FUNC(NetAddEventSTO);
[QGVAR(sm_p_pos), {GVAR(sm_p_pos) = _this}] call FUNC(NetAddEventToClients);
[QGVAR(mt_winner), {GVAR(mt_winner) = _this}] call FUNC(NetAddEventToClients);
[QGVAR(n_v), {_this call FUNC(initvec)}] call FUNC(NetAddEventToClients);
[QGVAR(m_l_o), {_this call FUNC(LightObj)}] call FUNC(NetAddEventToClients);
[2, QGVAR(mhqdepl), {if (local (_this select 0)) then {(_this select 0) lock (_this select 1)};_this call FUNC(mhqdeplNet)}] call FUNC(NetAddEvent);
[QGVAR(w_n), {[format [(localize "STR_DOM_MISSIONSTRING_506"), _this select 0, _this select 1], "GLOBAL"] call FUNC(HintChatMsg)}] call FUNC(NetAddEventToClients);
[QGVAR(tk_an), {
	[format [(localize "STR_DOM_MISSIONSTRING_507"), _this select 0, _this select 1], "GLOBAL"] call FUNC(HintChatMsg);
	if (serverCommandAvailable "#shutdown") then {serverCommand ("#kick " + (_this select 0))};
}] call FUNC(NetAddEventToClients);
[QGVAR(saat), {[format [localize "STR_DOM_MISSIONSTRING_1462", name _this], "GLOBAL"] call FUNC(HintChatMsg);}] call FUNC(NetAddEventToClients);
[2, QGVAR(say2), {if (alive player && {(player distance (_this select 0) < (_this select 2))}) then {(_this select 0) say3D (_this select 1)}}] call FUNC(NetAddEvent);
[QGVAR(em), {endMission "LOSER"}] call FUNC(NetAddEventSTO);
[QGVAR(ps_an), {
	switch (_this select 1) do {
		case 0: {[format [(localize "STR_DOM_MISSIONSTRING_508"), _this select 0], "GLOBAL"] call FUNC(HintChatMsg)};
		case 1: {[format [(localize "STR_DOM_MISSIONSTRING_509"), _this select 0], "GLOBAL"] call FUNC(HintChatMsg)};
	};
	if (serverCommandAvailable "#shutdown") then {serverCommand ("#kick " + (_this select 0))};
}] call FUNC(NetAddEventToClients);
[QGVAR(s_p_inf), {GVAR(u_r_inf) = _this}] call FUNC(NetAddEventSTO);
[QGVAR(w_ma), {deleteMarkerLocal _this}] call FUNC(NetAddEventToClients);
[2, QGVAR(p_o_a), {
	private "_ar";_ar = _this select 1;
#ifdef __TT__
	if (GVAR(player_side) == (_ar select 3)) then {
#endif
	if ((_ar select 0) isKindOf "Mash") then {
		[_ar select 1, getPosASL (_ar select 0),"ICON","ColorBlue",[0.5,0.5],format [(localize "STR_DOM_MISSIONSTRING_510"), _ar select 2],0,"mil_dot"] call FUNC(CreateMarkerLocal);
	} else {
		if ((_ar select 0) isKindOf "Base_WarfareBVehicleServicePoint") then {
			[_ar select 1, getPosASL (_ar select 0),"ICON","ColorBlue",[0.5,0.5],format [(localize "STR_DOM_MISSIONSTRING_511"), _ar select 2],0,"mil_dot"] call FUNC(CreateMarkerLocal);
		} else {
			[_ar select 1, getPosASL (_ar select 0),"ICON","ColorBlue",[0.5,0.5],format [(localize "STR_DOM_MISSIONSTRING_512"), _ar select 2],0,"mil_dot"] call FUNC(CreateMarkerLocal);
		};
	};
#ifdef __TT__
	};
#endif
}] call FUNC(NetAddEvent);
if (GVAR(with_ranked)) then {
	[QGVAR(pho), {(format [(localize "STR_DOM_MISSIONSTRING_288"), GVAR(ranked_a) select 17]) call FUNC(HQChat)}] call FUNC(NetAddEventSTO);
};
[2, QGVAR(p_o_r), {deleteMarkerLocal (_this select 1)}] call FUNC(NetAddEvent);
if (GVAR(engineerfull) == 0 || {GVAR(with_ai)} || {GVAR(with_ai_features) == 0}) then {
	[QGVAR(farp_e), {if (GVAR(eng_can_repfuel)) then {_this addAction [(localize "STR_DOM_MISSIONSTRING_513") call FUNC(BlueText), {_this call FUNC(restoreeng)}]}}] call FUNC(NetAddEventToClients);
};
[QGVAR(p_o_an), {_this call FUNC(PlacedObjAn)}] call FUNC(NetAddEventToClients);
#ifdef __TT__
if (GVAR(WithRevive) == 0) then {
	[QGVAR(u_k), {systemChat format [(localize "STR_DOM_MISSIONSTRING_514"), _this select 0, _this select 1, _this select 2, GVAR(tt_points) select 8]}] call FUNC(NetAddEventToClients);
} else {
	[QGVAR(u_k), {systemChat format [(localize "STR_DOM_MISSIONSTRING_515"), _this select 0, _this select 1, _this select 2, GVAR(tt_points) select 8]}] call FUNC(NetAddEventToClients);
};
[QGVAR(vec_killer), {systemChat format [(localize "STR_DOM_MISSIONSTRING_516"), _this select 0, _this select 1, _this select 2, GVAR(tt_points) select 7]}] call FUNC(NetAddEventToClients);
[QGVAR(r_mark), {if (GVAR(player_side) != (_this select 1)) then {_this spawn {scriptName "spawn_r_mark";waitUntil {((markerPos (_this select 0)) select 0) != 0};deleteMarkerLocal (_this select 0)}}}] call FUNC(NetAddEventToClients);
[QGVAR(attention), {_this call FUNC(dattention)}] call FUNC(NetAddEventToClients);
[QGVAR(w_m_c), {
	if (!isNil {_this select 3} && {!isNil QGVAR(player_side)} && {GVAR(player_side) == _this select 3}) then {
		[_this select 0, _this select 1,"ICON","ColorBlue",[1,1],format [(localize "STR_DOM_MISSIONSTRING_517"), _this select 2],0,"mil_triangle"] call FUNC(CreateMarkerLocal)
	}
}] call FUNC(NetAddEventToClients);
#else
[QGVAR(dropansw), {_this call FUNC(dropansw)}] call FUNC(NetAddEventSTO);
[QGVAR(n_jf), {if (GVAR(WithJumpFlags) == 1) then {_this call FUNC(newflagclient)}}] call FUNC(NetAddEventToClients);
[QGVAR(jet_sf), {_this call FUNC(jet_service_facNet)}] call FUNC(NetAddEventToClients);
[QGVAR(chop_sf), {_this call FUNC(chopper_service_facNet)}] call FUNC(NetAddEventToClients);
[QGVAR(wreck_rf), {_this call FUNC(wreck_repair_facNet)}] call FUNC(NetAddEventToClients);
[QGVAR(s_b_client), {__XJIPGetVar(GVAR(searchbody)) setVariable [QGVAR(search_id), __XJIPGetVar(GVAR(searchbody)) addAction [(localize "STR_DOM_MISSIONSTRING_518"), {_this spawn FUNC(searchbody)}]]}] call FUNC(NetAddEventToClients);
[QGVAR(rem_sb_id), {if (!isNil {__XJIPGetVar(GVAR(searchbody)) getVariable QGVAR(search_id)}) then {__XJIPGetVar(GVAR(searchbody)) removeAction (__XJIPGetVar(GVAR(searchbody)) getVariable QGVAR(search_id))}}] call FUNC(NetAddEventToClients);
[QGVAR(intel_upd), {_this call FUNC(intel_updNet)}] call FUNC(NetAddEventToClients);
[QGVAR(w_m_c), {[_this select 0, _this select 1,"ICON","ColorBlue",[1,1],format [(localize "STR_DOM_MISSIONSTRING_517"), _this select 2],0,"mil_triangle"] call FUNC(CreateMarkerLocal)}] call FUNC(NetAddEventToClients);
#endif
[QGVAR(smsg), {(localize "STR_DOM_MISSIONSTRING_519") call FUNC(HQChat)}] call FUNC(NetAddEventToClients);

[QGVAR(mqhtn), {[format [(localize "STR_DOM_MISSIONSTRING_520"), GVAR(MHQDisableNearMT), _this], "HQ"] call FUNC(HintChatMsg)}] call FUNC(NetAddEventToClients);

[QGVAR(ccso), {playSound "Ui_cc"}] call FUNC(NetAddEventToClients);

[QGVAR(grpswmsg), {systemChat ((_this select 1) + " " + localize "STR_DOM_MISSIONSTRING_1432")}] call FUNC(NetAddEventSTO);
[QGVAR(grpswmsgn), {systemChat ((_this select 1) + " " + localize "STR_DOM_MISSIONSTRING_1433")}] call FUNC(NetAddEventSTO);

[QGVAR(vboxrefi), {_this setVariable [QGVAR(boxcargo), nil]}] call FUNC(NetAddEventToClients);

[QGVAR(hidehc), {hideObject HC_D_UNIT}] call FUNC(NetAddEventToClients);

[2, QGVAR(setVideoPIPTar), {_this call FUNC(setVideoPipTarget)}] call FUNC(NetAddEvent);

[QGVAR(kbunits), {
	private "_unit";
	_unit = missionNamespace getVariable (_this select 0);
	_unit enableSimulation false;
	_unit hideObject true;
	_unit setVariable [QGVAR(noSimCheck), true];
	if (_this select 1 != side (group player)) exitWith {};
	_unit kbAddTopic["PL" + str(player),"x_bikb\domkba3.bikb"];
}] call FUNC(NetAddEventToClients);

[QGVAR(stocbike), {
	_vehicle = _this select 1;
	player reveal _vehicle;
	player moveInDriver _vehicle;

	if (_vehicle isKindOf "Quadbike_01_Base_F") then {
		_vehicle addAction [(localize "STR_DOM_MISSIONSTRING_162") call FUNC(BlueText), {_this call FUNC(flipatv)}, 0, -1, false, false, "", "!(player in _target) && {((vectorUp _target) select 2) < 0.6}"];
	};

	if (__pGetVar(GVAR(bike_b_mode)) == 1) then {
		_vehicle spawn {
			scriptName "spawn_x_bike_1";
			private "_vehicle";
			_vehicle = _this;
			waitUntil {sleep 0.412;!alive player || {!alive _vehicle}};
			sleep 10.123;
			while {true} do {
				if (_vehicle call FUNC(GetVehicleEmpty)) exitWith {deleteVehicle _vehicle};
				sleep 15.123;
			};
		};
	} else {
		GVAR(flag_vec) = _vehicle;
		GVAR(flag_vec) addEventHandler ["killed", {(_this select 0) spawn {private ["_vec"];_vec = _this;sleep 10.123;while {true} do {if (isNull _vec) exitWith {};if (_vec call FUNC(GetVehicleEmpty)) exitWith {deleteVehicle _vec};sleep 15.123};GVAR(flag_vec) = objNull}}];
	};
}] call FUNC(NetAddEventSTO);

if (GVAR(domdatabase)) then {
	[QGVAR(sendps), {_this call FUNC(CreatePStatsDialog)}] call FUNC(NetAddEventSTO);
};

// TYRGHEN - Made configurable
if (GVAR(ServerClientFPSDisplay) == 0) then {
	999123 cutRsc [QGVAR(fpsresource),"PLAIN"];
	[QGVAR(dfps), {
		disableSerialization;
		_disp = __uiGetVar(GVAR(fpsresource));
		if (!isNil "_disp") then {
			(_disp displayCtrl 50) ctrlSetText str(_this);
			(_disp displayCtrl 51) ctrlSetText str(diag_fps);
		};
	}] call FUNC(NetAddEventToClients);
};

0 spawn {
	scriptName "spawn_playerstuff";
	sleep 1 + random 3;
	if (isMultiplayer) then {
		[QGVAR(p_a), player] call FUNC(NetCallEventCTS);// ask the server for the client score, etc
		waitUntil {!GVAR(still_in_intro)};
		xr_phd_invulnerable = false;
		sleep 2;
		__pSetVar [QGVAR(player_old_rank), 0];
		["player_rank", {call FUNC(PlayerRank)},5.01] call FUNC(addPerFrame);
	} else {
		GVAR(player_autokick_time) = GVAR(AutoKickTime);
		xr_phd_invulnerable = false;
		sleep 20;
		if (GVAR(still_in_intro)) then {
			GVAR(still_in_intro) = false;
		};
	};
	// TYRGHEN moved from below.
	["init_vecs", {{_x call FUNC(initvec)} forEach vehicles;["init_vecs"] call FUNC(removePerFrame)},0] call FUNC(addPerFrame);
};

//TYRGHEN - commented
//["init_vecs", {{_x call FUNC(initvec)} forEach vehicles;["init_vecs"] call FUNC(removePerFrame)},0] call FUNC(addPerFrame);

// TODO
if (GVAR(with_ranked)) then {
	// basic rifle at start
	_weapp = "";
	_magp = "";
	switch (GVAR(own_side)) do {
		case "BLUFOR": {
			_weapp = "arifle_MX_F";
			_magp = "30Rnd_65x39_caseless_mag";
		};
		case "OPFOR": {
			_weapp = "AK_74";
			_magp = "30Rnd_545x39_AK";
		};
		case "INDEPENDENT": {
			_weapp = "M16A4";
			_magp = "30Rnd_556x45_Stanag";
		};
	};
	removeAllWeapons _p;
	_p addMagazines [_magp, 6];
	_p addWeapon _weapp;
};

#define __tctn _target_array = GVAR(target_names) select _res;\
_current_target_pos = _target_array select 0;\
_target_name = _target_array select 1;\
_target_radius = _target_array select 2
_taskstr = "d_task%2 = player createSimpleTask ['obj%2'];d_task%2 setSimpleTaskDescription ['" + localize "STR_DOM_MISSIONSTRING_202" + "','" + localize "STR_DOM_MISSIONSTRING_203" + "','" + localize "STR_DOM_MISSIONSTRING_203" + "'];d_task%2 settaskstate _objstatus;d_task%2 setSimpleTaskDestination _current_target_pos;";
#define __tmarker [_target_name, _current_target_pos,#ELLIPSE,_color,[_target_radius,_target_radius]] call FUNC(CreateMarkerLocal)
if (GVAR(MissionType) != 2) then {
#ifndef __TT__
	if (count __XJIPGetVar(resolved_targets) > 0) then {
		for "_i" from 0 to (count __XJIPGetVar(resolved_targets) - 1) do {
			if (isNil {__XJIPGetVar(resolved_targets)} || {_i >= count __XJIPGetVar(resolved_targets)}) exitWith {};
			_res = __XJIPGetVar(resolved_targets) select _i;
			if (!isNil "_res" && {_res >= 0}) then {
				__tctn;
				_mname = format [QGVAR(target_%1), _res];
				_no = __getMNsVar2(_mname);
				_color = "ColorGreen";
				_objstatus = "Succeeded";
				if (!isNull _no) then {
					_isrec = GV(_no,GVAR(recaptured));
					if (!isNil "_isrec") then {
						_objstatus = "Failed";
						_color = "ColorRed";
						[_target_name, _current_target_pos,"ELLIPSE",_color,[_target_radius,_target_radius],"",0,"Marker","FDiagonal"] call FUNC(CreateMarkerLocal);
					} else {
						__tmarker;
					};
				} else {
					__tmarker;
				};

				call compile format [_taskstr,_target_name,_res + 2];
			};
		};
	};
#else
	if (count __XJIPGetVar(resolved_targets) > 0) then {
		for "_i" from 0 to (count __XJIPGetVar(resolved_targets) - 1) do {
			if (isNil {__XJIPGetVar(resolved_targets)} || {_i == count __XJIPGetVar(resolved_targets)}) exitWith {};
			_xres = __XJIPGetVar(resolved_targets) select _i;
			_res = _xres select 0;
			_winner = _xres select 1;
			__tctn;
			_color = switch (_winner) do {
				case 1: {"ColorBlue"};
				case 2: {"ColorRed"};
				case 3: {"ColorGreen"};
			};
			__tmarker;
			call compile format [_taskstr,_target_name,_res + 2];
		};
	};
#endif

	GVAR(current_seize) = "";
	if (__XJIPGetVar(GVAR(current_target_index)) != -1 && {!__XJIPGetVar(target_clear)}) then {
		__TargetInfo;
		_current_target_pos = _target_array2 select 0;
		GVAR(current_seize) = _current_target_name;
		_target_radius = _target_array2 select 2;	
	#ifndef __TT__
		_color = "ColorRed";
	#else
		_color = "ColorYellow";
	#endif
		[_current_target_name, _current_target_pos,"ELLIPSE",_color,[_target_radius,_target_radius]] call FUNC(CreateMarkerLocal);
		QGVAR(dummy_marker) setMarkerPosLocal _current_target_pos;
		_objstatus = "Created";
		call compile format [(_taskstr + "d_current_task = d_task%2;"), _current_target_name, __XJIPGetVar(GVAR(current_target_index)) + 2];
	};
	// TYRGHEN - Create the priority target task and marker if needed
	false spawn FUNC(createprioritytargetclient);
};

{
	if (typeName _x == "ARRAY") then {
#ifdef __TT__
		if (GVAR(player_side) == (_x select 3)) then {
#endif
		[_x select 0, _x select 1,"ICON","ColorBlue",[1,1],format [(localize "STR_DOM_MISSIONSTRING_517"), _x select 2],0,"mil_triangle"] call FUNC(CreateMarkerLocal);
#ifdef __TT__
		};
#endif
	};
} forEach __XJIPGetVar(GVAR(wreck_marker));

if (GVAR(MissionType) != 2) then {
	_counterxx = 0;
	_markertxt = if (GVAR(jumpflag_vec) == "") then {"Parajump"} else {"Vehicle"};
	{
		// TYRGHEN
		[_x, format [QGVAR(paraflag%1), _counterxx]] call FUNC(newflagclient);
		__INC(_counterxx);
		// _pos = position _x;
		// [format [QGVAR(paraflag%1), _counterxx], _pos,"ICON","ColorYellow",[0.5,0.5],_markertxt,0,"mil_flag"] call FUNC(CreateMarkerLocal);
		// __INC(_counterxx);
		// if (GVAR(jumpflag_vec) == "") then {
			// _x addaction [(localize "STR_DOM_MISSIONSTRING_296") call FUNC(BlueText),{_this spawn FUNC(paraj)}];
		// } else {
			// _text = format [(localize "STR_DOM_MISSIONSTRING_297"), [GVAR(jumpflag_vec),0] call FUNC(GetDisplayName)];
			// _x addAction [_text call FUNC(BlueText),{_this spawn FUNC(bike)},[GVAR(jumpflag_vec),1]];
		// };
// #ifdef __ACE__
		// if (GVAR(jumpflag_vec) == "") then {
			// _box = "ACE_RuckBox" createVehicleLocal _pos;
			// clearMagazineCargo _box;
			// clearWeaponCargo _box;
			// _box addweaponcargo ["ACE_ParachutePack",10];
			// _box allowDamage false;
		// };
// #endif
	} forEach __XJIPGetVar(jump_flags);
};

if (GVAR(MissionType) != 2 && {!__XJIPGetVar(GVAR(mt_radio_down))} && {__XJIPGetVar(mt_radio_pos) select 0 != 0}) then {
	// TYRGHEN
	if (GVAR(towerpointmarker) != "") then {
		[QGVAR(main_target_radiotower), __XJIPGetVar(mt_radio_pos),"ICON","ColorBlack",[0.5,0.5],(localize "STR_DOM_MISSIONSTRING_521"),0,GVAR(towerpointmarker)] call FUNC(CreateMarkerLocal);
	};
};

if (GVAR(MissionType) != 2 && {count __XJIPGetVar(GVAR(currentcamps)) > 0}) then {
	_owncol = switch (GVAR(own_side)) do {
		case "BLUFOR": { "ColorBlufor" };
		case "OPFOR": { "ColorOpfor" };
		case "INDEPENDENT": { "ColorGreen" };
	};
	{
		if (!isNull _x) then {
#ifndef __TT__
			_mcol = if (GV(_x,GVAR(SIDE)) == GVAR(own_side)) then {_owncol} else {"ColorBlack"};
#else
			_mcol = switch (GV(_x,GVAR(SIDE))) do {
				case "BLUFOR": {"ColorBlufor"};
				case "OPFOR": {"ColorOpfor"};
				case "INDEPENDENT": {"ColorBlack"};
			};
#endif
			// TYRGHEN
			if (GVAR(strongpointmarker) != "") then {
				[format[QGVAR(camp%1),GV(_x,GVAR(INDEX))], getPosASL _x,"ICON",_mcol,[0.5,0.5],"",0,GVAR(strongpointmarker)] call FUNC(CreateMarkerLocal);
			};
			// TYRGHEN Added flag update
			_curside = _x getVariable QGVAR(SIDE);
			_flag = _x getVariable QGVAR(FLAG);
			switch (_curside) do {
				case GVAR(enemy_side): {
					_texture = call FUNC(getenemyflagtex);
				};
				case GVAR(own_side): {
					_texture = call FUNC(getownflagtex);
				};
			};
			_flag setFlagTexture _texture;
		};
	} forEach __XJIPGetVar(GVAR(currentcamps));
};

if (__XJIPGetVar(all_sm_res)) then {GVAR(cur_sm_txt) = localize "STR_DOM_MISSIONSTRING_522"} else {[false] spawn FUNC(getsidemissionclient)};

#define __paddweap(xweap) if (!(_p hasWeapon #xweap)) then {_p addWeapon #xweap}
if (GVAR(without_player_nvg) == 1) then {
	if !(player call FUNC(hasnvgoggles)) then {
		player linkItem (switch (GVAR(player_side)) do {
			case opfor: {"NVGoggles_OPFOR"};
			case independent: {"NVGoggles_INDEP"};
			default {"NVGoggles"};
		});
	};
} else {
	player call FUNC(removeNVGoggles);
	execFSM "fsms\RemoveGoogles.fsm";
};
_weapop = weapons player;
if (!(player hasWeapon "Binocular") && {!("Binocular_Vector" in _weapop)} && {!("Laserdesignator" in _weapop)}) then {
	__paddweap(Binocular);
};
__paddweap(ItemGPS);

// TODO if this works with NVGoggles_OPFOR and NVGoggles_INDEP too
if (sunOrMoon == 0 && {GVAR(without_nvg) == 1}) then {_p action ["NVGoggles",_p]};

GVAR(pl_killer) = objNull;
GVAR(pl_killer_t) = 0;
GVAR(dublicate_kill) = false;
if (GVAR(with_ai)) then {
	player addMPEventHandler ["MPKilled", {_this call FUNC(x_checkkill)}];
} else {
#ifndef __TT__
	player addMPEventHandler ["MPKilled", {_this call FUNC(x_checkkill)}];
#else
	if (GVAR(player_side) == blufor) then {
			player addMPEventHandler ["MPKilled", {_this call FUNC(x_checkkillblufor)}];
	} else {
			player addMPEventHandler ["MPKilled", {_this call FUNC(x_checkkillopfor)}];
	};
#endif
};

player addEventHandler ["respawn", {_this call FUNC(prespawned)}];

if (count __XJIPGetVar(GVAR(ammo_boxes)) > 0) then {
	private ["_box_pos", "_boxnew", "_boxscript"];
	{
		if (typeName _x == "ARRAY") then {
			_box_pos = _x select 0;
#ifndef __TT__
			if ((_x select 1) != "") then {[_x select 1, _box_pos,"ICON","ColorBlue",[0.5,0.5],(localize "STR_DOM_MISSIONSTRING_523"),0,GVAR(dropped_box_marker)] call FUNC(CreateMarkerLocal)};
#else
			if ((_x select 1) != "" && {GVAR(player_side) == (_x select 2)}) then {[_x select 1, _box_pos,"ICON","ColorBlue",[0.5,0.5],(localize "STR_DOM_MISSIONSTRING_523"),0,GVAR(dropped_box_marker)] call FUNC(CreateMarkerLocal)};
#endif
			_boxnew = GVAR(the_box) createVehicleLocal _box_pos;
			_boxnew setPos _box_pos;
			//_boxnew addAction [(localize "STR_DOM_MISSIONSTRING_300") call FUNC(BlueText), {_this call FUNC(savelayout)}];
			//_boxnew addAction [(localize "STR_DOM_MISSIONSTRING_301") call FUNC(BlueText), {_this call FUNC(clearlayout)}];
			// TYRGHEN
			//[_boxnew] call FUNC(weaponcargo);
			[_boxnew] call FUNC(init_boxNet);
			_boxnew allowDamage false;
		};
	} forEach __XJIPGetVar(GVAR(ammo_boxes));
};

GVAR(player_can_call_drop) = 0;
GVAR(player_can_call_arti) = 0;
// TYRGHEN - Added taxi
GVAR(player_can_call_taxi) = 0;
GVAR(player_can_build_trench) = true;
__pSetVar [QGVAR(isinaction), false];

addMissionEventHandler ["Draw3D", {
#ifndef __TT__
	_pos = position GVAR(vecre_trigger);
	_distp = player distance _pos;
	if (_distp < 100) then {
		_scale = 0.032 - (_distp / 9000);
		_pos set [2, 5 + (_distp * 0.05)];
		_alpha = 1 - (_distp / 200);
		drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_524"), 1, _scale, "TahomaB"];
	};
	_pos = position GVAR(jet_trigger);
	_distp = player distance _pos;
	if (_distp < 100) then {
		_scale = 0.032 - (_distp / 9000);
		_pos set [2, 5 + (_distp * 0.05)];
		_alpha = 1 - (_distp / 200);
		drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_526"), 1, _scale, "TahomaB"];
	};
	_pos = position GVAR(chopper_trigger);
	_distp = player distance _pos;
	if (_distp < 100) then {
		_scale = 0.032 - (_distp / 9000);
		_pos set [2, 5 + (_distp * 0.05)];
		_alpha = 1 - (_distp / 200);
		drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_528"), 1, _scale, "TahomaB"];
	};
	_pos = position GVAR(wreck_rep);
	_distp = player distance _pos;
	if (_distp < 100) then {
		_scale = 0.032 - (_distp / 9000);
		_pos set [2, 5 + (_distp * 0.05)];
		_alpha = 1 - (_distp / 200);
		drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_0"), 1, _scale, "TahomaB"];
	};
	_pos = position GVAR(AMMOLOAD);
	_distp = player distance _pos;
	if (_distp < 100) then {
		_scale = 0.032 - (_distp / 9000);
		_pos set [2, 5 + (_distp * 0.05)];
		_alpha = 1 - (_distp / 200);
		drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_531"), 1, _scale, "TahomaB"];
	};
#else
	if (GVAR(player_side) == blufor) then {
		_pos = position GVAR(vecre_trigger);
		_distp = player distance _pos;
		if (_distp < 100) then {
			_scale = 0.032 - (_distp / 9000);
			_pos set [2, 5 + (_distp * 0.05)];
			_alpha = 1 - (_distp / 200);
			drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_524"), 1, _scale, "TahomaB"];
		};
		_pos = position GVAR(jet_trigger);
		_distp = player distance _pos;
		if (_distp < 100) then {
			_scale = 0.032 - (_distp / 9000);
			_pos set [2, 5 + (_distp * 0.05)];
			_alpha = 1 - (_distp / 200);
			drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_526"), 1, _scale, "TahomaB"];
		};
		_pos = position GVAR(chopper_trigger);
		_distp = player distance _pos;
		if (_distp < 100) then {
			_scale = 0.032 - (_distp / 9000);
			_pos set [2, 5 + (_distp * 0.05)];
			_alpha = 1 - (_distp / 200);
			drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_528"), 1, _scale, "TahomaB"];
		};
		_pos = position GVAR(wreck_rep);
		_distp = player distance _pos;
		if (_distp < 100) then {
			_scale = 0.032 - (_distp / 9000);
			_pos set [2, 5 + (_distp * 0.05)];
			_alpha = 1 - (_distp / 200);
			drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_0"), 1, _scale, "TahomaB"];
		};
		_pos = position GVAR(AMMOLOAD);
		_distp = player distance _pos;
		if (_distp < 100) then {
			_scale = 0.032 - (_distp / 9000);
			_pos set [2, 5 + (_distp * 0.05)];
			_alpha = 1 - (_distp / 200);
			drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_531"), 1, _scale, "TahomaB"];
		};
	} else {
		_pos = position GVAR(vecre_trigger2);
		_distp = player distance _pos;
		if (_distp < 100) then {
			_scale = 0.032 - (_distp / 9000);
			_pos set [2, 5 + (_distp * 0.05)];
			_alpha = 1 - (_distp / 200);
			drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_524"), 1, _scale, "TahomaB"];
		};
		_pos = position GVAR(jet_trigger2);
		_distp = player distance _pos;
		if (_distp < 100) then {
			_scale = 0.032 - (_distp / 9000);
			_pos set [2, 5 + (_distp * 0.05)];
			_alpha = 1 - (_distp / 200);
			drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_526"), 1, _scale, "TahomaB"];
		};
		_pos = position GVAR(chopper_triggerR);
		_distp = player distance _pos;
		if (_distp < 100) then {
			_scale = 0.032 - (_distp / 9000);
			_pos set [2, 5 + (_distp * 0.05)];
			_alpha = 1 - (_distp / 200);
			drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_528"), 1, _scale, "TahomaB"];
		};
		_pos = position GVAR(wreck_rep2);
		_distp = player distance _pos;
		if (_distp < 100) then {
			_scale = 0.032 - (_distp / 9000);
			_pos set [2, 5 + (_distp * 0.05)];
			_alpha = 1 - (_distp / 200);
			drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_0"), 1, _scale, "TahomaB"];
		};
		_pos = position GVAR(AMMOLOAD2);
		_distp = player distance _pos;
		if (_distp < 100) then {
			_scale = 0.032 - (_distp / 9000);
			_pos set [2, 5 + (_distp * 0.05)];
			_alpha = 1 - (_distp / 200);
			drawIcon3D ["#(argb,8,8,3)color(0,0,0,0)", [0,0,1,_alpha], _pos, 1, 1, 0, (localize "STR_DOM_MISSIONSTRING_531"), 1, _scale, "TahomaB"];
		};
	};
#endif
}];

diag_log ["Internal D Version:",__DOM_NVER_STR2__];

_p addRating 20000;

__pSetVar [QGVAR(trench), objNull];
__pSetVar [QGVAR(trenchid), -9999];
if (GVAR(with_ai) || {GVAR(with_ai_features) == 0}) then {
	if (GVAR(with_ai)) then {
		if (isNil {__XJIPGetVar(GVAR(AI_HUT))}) then {
			0 spawn {
				scriptName "spawn_wait_for_ai_hut";
				waitUntil {sleep 0.512;!isNil {__XJIPGetVar(GVAR(AI_HUT))}};
				__ccppfln(x_client\x_recruitsetup.sqf);
			};
		} else {
			__ccppfln(x_client\x_recruitsetup.sqf);
		};
		
		_grpp = group player;
		_leader = leader _grpp;
		if (!isPlayer _leader || {player == _leader}) then {
			{
				if (!isPlayer _x) then {
					if (vehicle _x == _x) then {
						deleteVehicle _x;
					} else {
						moveOut _x;
						_x spawn {
							scriptName "spawn_ai_vecout";
							waitUntil {sleep 0.331;vehicle _this == _this};
							deleteVehicle _this;
						};
					};
				};
			} forEach units _grpp;
		};
	};
};
// TYRGHEN - Changed all tests to make for a cleaner code, centralising everything
if (GVAR(string_player) in GVAR(can_call_drop_ar)) then {
	GVAR(player_can_call_drop) = 1;
};
if (GVAR(string_player) in GVAR(can_use_artillery)) then {
	GVAR(player_can_call_arti) = 1;
	if (GVAR(string_player) == "RESCUE2") then {
		GVAR(player_can_call_arti) = 2;
	};
};
GVAR(player_can_build_trench) = true;
// TYRGHEN - Added Taxi
if (GVAR(string_player) in GVAR(can_call_taxi_ar)) then {
	GVAR(player_can_call_taxi) = 1;
};

_respawn_marker = "";
#define __dml_w deleteMarkerLocal #respawn_west
#define __dml_e deleteMarkerLocal #respawn_east
#define __dml_g deleteMarkerLocal #respawn_guerrila
switch (GVAR(own_side)) do {
	case "INDEPENDENT": {
		_respawn_marker = "respawn_guerrila";
		__dml_w;
		__dml_e;
	};
	case "BLUFOR": {
		_respawn_marker = "respawn_west";
		__dml_g;
		__dml_e;
	};
	case "OPFOR": {
		_respawn_marker = "respawn_east";
		__dml_w;
		__dml_g;
	};
};

#define __rmsmpl _respawn_marker setMarkerPosLocal
#ifdef __TT__
if (GVAR(player_side) == blufor) then {
	__rmsmpl markerPos "base_spawn_1";
} else {
	__rmsmpl markerPos "base_spawn_2";
};
#else
__rmsmpl markerPos "base_spawn_1";
#endif

__pSetVar [QGVAR(pbp_id), -9999];
GVAR(backpack_helper) = [];
__pSetVar [QGVAR(custom_backpack), []];
__pSetVar [QGVAR(player_backpack), []];
if (GVAR(WithBackpack)) then {
	GVAR(prim_weap_player) = primaryWeapon _p;
	_s = format [(localize "STR_DOM_MISSIONSTRING_155"), [GVAR(prim_weap_player),1] call FUNC(GetDisplayName)];
	if (GVAR(prim_weap_player) != "" && {GVAR(prim_weap_player) != " "}) then {
		__pSetVar [QGVAR(pbp_id), _p addAction [_s call FUNC(GreyText), {_this spawn FUNC(backpack)},[],-1,false,true,"","!surfaceIsWater (getPosASL _target) && {vehicle _target == _target} && {(getPos _target) select 2 < 2} && {!(_target getVariable 'xr_pluncon')}"]];
	};
	// No Weapon fix for backpack
	[_pos, [0, 0, 0, false], ["NONE", "PRESENT", true], ["primaryWeapon player != d_prim_weap_player && {primaryWeapon player != ' '} && {!dialog}","call {d_prim_weap_player = primaryWeapon player;_id = player getVariable 'd_pbp_id';if (_id != -9999 && count (player getVariable 'd_player_backpack') == 0) then {player removeAction _id;player setVariable ['d_pbp_id', -9999]};if ((player getVariable 'd_pbp_id' == -9999) && count (player getVariable 'd_player_backpack') == 0 && d_prim_weap_player != '' && d_prim_weap_player != ' ') then {player setVariable ['d_pbp_id', player addAction [format [localize 'STR_DOM_MISSIONSTRING_155', [d_prim_weap_player,1] call d_fnc_GetDisplayName] call d_fnc_GreyText, {_this spawn d_fnc_backpack},[],-1,false,true,'','!surfaceIsWater (getPosASL _target) && {vehicle _target == _target} && {(getPos _target) select 2 < 2} && {!(_target getVariable ''xr_pluncon'')}']]}}",""]] call FUNC(CreateTrigger);
};

#ifndef __TT__
GVAR(base_trigger) = createTrigger["EmptyDetector" ,GVAR(base_array) select 0];
GVAR(base_trigger) setTriggerArea [GVAR(base_array) select 1, GVAR(base_array) select 2, GVAR(base_array) select 3, true];
#else
_dbase_a = if (GVAR(player_side) == blufor) then {GVAR(base_array) select 0} else {GVAR(base_array) select 1};
GVAR(base_trigger) = createTrigger["EmptyDetector" ,_dbase_a select 0];
GVAR(base_trigger) setTriggerArea [_dbase_a select 1, _dbase_a select 2, _dbase_a select 3, true];
#endif
GVAR(base_trigger) setTriggerActivation [GVAR(own_side_trigger), "PRESENT", true];
GVAR(base_trigger) setTriggerStatements["this", "", ""];

// special triggers for engineers, AI version, everybody can repair and flip vehicles
GVAR(eng_can_repfuel) = false;
if (GVAR(string_player) in GVAR(is_engineer) || {GVAR(with_ai)} || {GVAR(with_ai_features) == 0}) then {
	GVAR(eng_can_repfuel) = true;

	if (GVAR(engineerfull) == 0 || {GVAR(with_ai)} || {GVAR(with_ai_features) == 0}) then {
#ifndef __TT__
		GVAR(engineer_trigger) = createTrigger["EmptyDetector" ,GVAR(base_array) select 0];
		GVAR(engineer_trigger) setTriggerArea [GVAR(base_array) select 1, GVAR(base_array) select 2, GVAR(base_array) select 3, true];
#else
		_dbase_a = if (playerSide == blufor) then {GVAR(base_array) select 0} else {GVAR(base_array) select 1};
		GVAR(engineer_trigger) = createTrigger["EmptyDetector" ,_dbase_a select 0];
		GVAR(engineer_trigger) setTriggerArea [_dbase_a select 1, _dbase_a select 2, _dbase_a select 3, true];
#endif
		GVAR(engineer_trigger) setTriggerActivation [GVAR(own_side_trigger), "PRESENT", true];
		GVAR(engineer_trigger) setTriggerStatements["!d_eng_can_repfuel && {player in thislist}", "d_eng_can_repfuel = true;systemChat (localize 'STR_DOM_MISSIONSTRING_340')", ""];
	};
	
	if (GVAR(with_ranked)) then {GVAR(last_base_repair) = -1};
	
	//TYRGHEN NEARESTTAG - trying with nearestObjects
	[_pos, [0, 0, 0, false], ["NONE", "PRESENT", true], ["call d_fnc_ffunc", "actionID1=player addAction [(localize 'STR_DOM_MISSIONSTRING_1408') call d_fnc_GreyText, {_this call d_fnc_unflipVehicle},[d_objectID1],-1,false];", "player removeAction actionID1"]] call FUNC(CreateTrigger);
	
	//TYRGHEN NEARESTTAG - Trying with nearestObjects
	if (GVAR(engineerfull) == 0 || {GVAR(with_ai)} || {GVAR(with_ai_features) == 0}) then {
		//TYRGHEN - Add analyze (available on all vecs) + added translation
		_trigger = createTrigger["EmptyDetector" ,_pos];
		_trigger setTriggerArea [0, 0, 0, true];
		_trigger setTriggerActivation ["NONE", "PRESENT", true];
		_trigger setTriggerStatements["call d_fnc_afunc", "d_actionID6 = player addAction [(localize 'STR_DOM_MISSIONSTRING_2030') call d_fnc_GreyText, {_this call d_fnc_repanalyze},[],-1,false];", "player removeAction d_actionID6;"];

		_trigger = createTrigger["EmptyDetector" ,_pos];
		_trigger setTriggerArea [0, 0, 0, true];
		_trigger setTriggerActivation ["NONE", "PRESENT", true];
#ifndef __ENGINEER_OLD__
		// TYRGHEN - Removed add analyze so it works on non damaged vecs as well + added translation
		_trigger setTriggerStatements["call d_fnc_sfunc", "d_actionID2 = player addAction [(localize 'STR_DOM_MISSIONSTRING_2031') call d_fnc_GreyText, {_this spawn d_fnc_repengineer},[],-1,false]", "player removeAction d_actionID2"];
#else
		_trigger setTriggerStatements["call d_fnc_sfunc", "d_actionID2 = player addAction [(localize 'STR_DOM_MISSIONSTRING_2031') call d_fnc_GreyText, {_this spawn d_fnc_repengineer_old},[],-1,false]", "player removeAction d_actionID2"];
#endif
	};
	
	__pSetVar [QGVAR(is_engineer),true];
	__pSetVar [QGVAR(farp_pos), []];
	
	if (GVAR(engineerfull) == 0 || {GVAR(with_ai)} || {GVAR(with_ai_features) == 0}) then {
		{_x addAction [(localize "STR_DOM_MISSIONSTRING_513") call FUNC(BlueText), {_this call FUNC(restoreeng)}]} forEach (__XJIPGetVar(GVAR(farps)));
	};
};


GVAR(there_are_enemies_atbase) = false;
GVAR(enemies_near_base) = false;
#ifndef __TT__
// Enemy at base
"enemy_at_base" setMarkerPosLocal (GVAR(base_array) select 0);
"enemy_at_base" setMarkerDirLocal (GVAR(base_array) select 3);
[GVAR(base_array) select 0, [GVAR(base_array) select 1, GVAR(base_array) select 2, GVAR(base_array) select 3, true], [GVAR(enemy_side_trigger), "PRESENT", true], ["'Man' countType thislist > 0 || {'Tank' countType thislist > 0} || {'Car' countType thislist > 0}", "[0] call d_fnc_BaseEnemies;'enemy_at_base' setMarkerSizeLocal [d_base_array select 1,d_base_array select 2];d_there_are_enemies_atbase = true", "[1] call d_fnc_BaseEnemies;'enemy_at_base' setMarkerSizeLocal [0,0];d_there_are_enemies_atbase = false"]] call FUNC(CreateTrigger);
[GVAR(base_array) select 0, [(GVAR(base_array) select 1) + 300, (GVAR(base_array) select 2) + 300, GVAR(base_array) select 3, true], [GVAR(enemy_side_trigger), "PRESENT", true], ["'Man' countType thislist > 0 || {'Tank' countType thislist > 0} || {'Car' countType thislist > 0}", "hint (localize 'STR_DOM_MISSIONSTRING_1409');d_enemies_near_base = true", "d_enemies_near_base = false"]] call FUNC(CreateTrigger);
#endif

GVAR(player_can_build_mgnest) = false;
if (GVAR(with_mgnest) && {GVAR(string_player) in GVAR(can_use_mgnests)}) then {
	GVAR(player_can_build_mgnest) = true;
	__pSetVar [QGVAR(mgnest_pos), []];
};

if (isSteamMission) exitWith {endMission "LOSER"};

GVAR(player_is_medic) = false;
if (GVAR(string_player) in GVAR(is_medic) && {GVAR(with_medtent)}) then {
	GVAR(player_is_medic) = true;
	__pSetVar [QGVAR(medtent), []];
};

#ifndef __TT__
if (GVAR(MissionType) != 2) then {
	if (GVAR(string_player) in GVAR(is_engineer) || {GVAR(with_ai)} || {GVAR(with_ai_features) == 0}) then {
		if (__XJIPGetVar(GVAR(jet_serviceH)) && {!__XJIPGetVar(GVAR(jet_s_reb))}) then {
			[0] spawn FUNC(XFacAction);
		};
		if (__XJIPGetVar(GVAR(chopper_serviceH)) && {!__XJIPGetVar(GVAR(chopper_s_reb))}) then {
			[1] spawn FUNC(XFacAction);
		};
		if (__XJIPGetVar(GVAR(wreck_repairH)) && {!__XJIPGetVar(GVAR(wreck_s_reb))}) then {
			[2] spawn FUNC(XFacAction);
		};
	};

// #define __facset2 _pos = _element select 0;\
// _dir = _element select 1;\
// _fac = "Land_vez_ruins" createVehicleLocal _pos;\
// _fac setDir _dir
	// if (__XJIPGetVar(GVAR(jet_serviceH)) && {!__XJIPGetVar(GVAR(jet_s_reb))}) then {
		// _element = GVAR(aircraft_facs) select 0;
		// __facset2;
	// };
	// if (__XJIPGetVar(GVAR(chopper_serviceH)) && {!__XJIPGetVar(GVAR(chopper_s_reb))}) then {
		// _element = GVAR(aircraft_facs) select 1;
		// __facset2;
	// };
	// if (__XJIPGetVar(GVAR(wreck_repairH)) && {!__XJIPGetVar(GVAR(wreck_s_reb))}) then {
		// _element = GVAR(aircraft_facs) select 2;
		// __facset2;
	// };

};
#endif

// TYRGHEN - We do not make it exclusive
//if (GVAR(WithJumpFlags) == 0) then {GVAR(ParaAtBase) = 1};

_tactionar = [(localize "STR_DOM_MISSIONSTRING_533") call FUNC(BlueText),{_this call FUNC(x_teleport)}];
#ifndef __TT__
if (GVAR(WithMHQTeleport) == 0) then {
	GVAR(FLAG_BASE) addAction _tactionar;
};
if (GVAR(with_ai) || {(GVAR(ParaAtBase) == 0)}) then {
	GVAR(FLAG_BASE) addAction [(localize "STR_DOM_MISSIONSTRING_296") call FUNC(BlueText),{_this spawn FUNC(paraj)}];
};
#else
if (GVAR(WithMHQTeleport) == 0) then {
	(if (GVAR(own_side) == "BLUFOR") then {GVAR(WFLAG_BASE)} else {GVAR(EFLAG_BASE)}) addAction _tactionar;
};
#endif

if (GVAR(ParaAtBase) == 1) then {
	_s = QGVAR(Teleporter);
	_sn = (localize "STR_DOM_MISSIONSTRING_534");
#ifndef __TT__
	_s setMarkerTextLocal _sn;
#else
	if (GVAR(own_side) == "BLUFOR") then {
		_s setMarkerTextLocal _sn;
	} else {
		QGVAR(teleporter_1) setMarkerTextLocal _sn;
	};
#endif
};

if (GVAR(with_ranked)) then {
	player addEventHandler ["handleHeal", {_this call FUNC(HandleHeal)}];
	//if (GVAR(string_player) in GVAR(is_medic)) then {execVM "x_client\x_mediccheck.sqf"};
};

if ((GVAR(with_ai) || {GVAR(with_ai_features) == 0}) && {GVAR(player_can_call_taxi) > 0}) then {
	GVAR(heli_taxi_available) = true;
	// TYRGHEN - Added TyrMenu Test
	if (isNil "TYR_MENU_IsActive" || {TYR_MENU_IsActive <= 0}) then {
		_trigger = createTrigger ["EmptyDetector", _pos];
		_trigger setTriggerText (localize "STR_DOM_MISSIONSTRING_535");
		_trigger setTriggerActivation ["HOTEL", "PRESENT", true];
		_trigger setTriggerStatements ["this", "0 = 0 spawn d_fnc_airtaxi",""];
	};
};

GVAR(vec_end_time) = -1;

if (isMultiplayer) then {
	0 spawn {
		scriptName "spawn_sendplayerstuff";
		sleep (0.5 + random 2);
		[QGVAR(p_varn), [getPlayerUID player,GVAR(string_player),GVAR(player_side)]] call FUNC(NetCallEventCTS);
	};
	// TYRGHEN - Stupid fix for BIS being assholes who broke variable synchornisation into their own namespace...
	{ missionNameSpace setVariable [str(_x),_x];
	} foreach playableUnits
};

#ifdef __TT__
if (GVAR(player_side) == opfor) then {
	QGVAR(arti_target) setMarkerAlphaLocal 0;
	QGVAR(arti_target2) setMarkerTextLocal (localize "STR_DOM_MISSIONSTRING_536");
} else {
	QGVAR(arti_target2) setMarkerAlphaLocal 0;
	QGVAR(arti_target) setMarkerTextLocal (localize "STR_DOM_MISSIONSTRING_536");
};
#endif

if (GVAR(LimitedWeapons)) then {
	GVAR(poss_weapons) = [];
	for "_i" from 0 to (count GVAR(limited_weapons_ar) - 2) do {
		_ar = GVAR(limited_weapons_ar) select _i;
		if (GVAR(string_player) in (_ar select 0)) exitWith {GVAR(poss_weapons) =+ _ar select 1};
	};
	if (count GVAR(poss_weapons) == 0) then {GVAR(poss_weapons) =+ (GVAR(limited_weapons_ar) select (count GVAR(limited_weapons_ar) - 1)) select 1};
	execFSM "fsms\LimitWeapons.fsm";
	GVAR(limited_weapons_ar) = nil;
};

execVM "x_msg\x_playernamehud.sqf";

if (GVAR(MissionType) != 2) then {
	execFSM "fsms\CampDialog.fsm";
};

if (GVAR(with_ai)) then {
	0 spawn {
		scriptName "spawn_withaicheck";
		while {true} do {
			waitUntil {sleep 0.272;alive player};
			if (player != leader (group player) && {!__pGetVar(xr_pluncon)} && {count GVAR(current_ai_units) > 0}) then {
				GVAR(current_ai_units) = [];
				GVAR(current_ai_num) = 0;
			};
			if (__pGetVar(xr_pluncon)) then {
				waitUntil {sleep 0.332;!__pGetVar(xr_pluncon) || {!alive player}};
			};
			sleep 1.212;
		};
	};
};

_primw = primaryWeapon _p;
if (_primw != "") then {
	_p selectWeapon _primw;
	_muzzles = getArray(configFile/"cfgWeapons"/_primw/"muzzles");
	_p selectWeapon (_muzzles select 0);
};

#ifndef __TT__
if (GVAR(MissionType) != 2) then {
	_sb = __XJIPGetVar(GVAR(searchbody));
	if (!isNull _sb && {isNil {GV(_sb,GVAR(search_body))}}) then {
		_sb setVariable [QGVAR(search_id), _sb addAction [(localize "STR_DOM_MISSIONSTRING_518"), {_this spawn FUNC(searchbody)}]];
	};
};
#endif

player addEventHandler ["fired", {_this call FUNC(ParaExploitHandler)}];

__pSetVar [QGVAR(p_f_b), 0];

#ifndef __TT__
GVAR(player_base_trig) = createTrigger["EmptyDetector" ,GVAR(base_array) select 0];
GVAR(player_base_trig) setTriggerArea [GVAR(base_array) select 1, GVAR(base_array) select 2, GVAR(base_array) select 3, true];
#else
_dbase_a = if (GVAR(player_side) == blufor) then {GVAR(base_array) select 0} else {GVAR(base_array) select 1};
GVAR(player_base_trig) = createTrigger["EmptyDetector" ,_dbase_a select 0];
GVAR(player_base_trig) setTriggerArea [_dbase_a select 1, _dbase_a select 2, _dbase_a select 3, true];
#endif
GVAR(player_base_trig) setTriggerActivation [GVAR(own_side_trigger), "PRESENT", true];
GVAR(player_base_trig) setTriggerStatements["this", "", ""];

#ifndef __TT__
GVAR(player_base_trig2) = createTrigger["EmptyDetector" ,position GVAR(FLAG_BASE)];
#else
_dbase_a = if (GVAR(player_side) == blufor) then {position GVAR(WFLAG_BASE)} else {position GVAR(EFLAG_BASE)};
GVAR(player_base_trig2) = createTrigger["EmptyDetector" ,_dbase_a];
#endif
GVAR(player_base_trig2) setTriggerArea [25, 25, 0, false];
GVAR(player_base_trig2) setTriggerActivation [GVAR(own_side_trigger), "PRESENT", true];
GVAR(player_base_trig2) setTriggerStatements["this", "", ""];

player addEventHandler ["fired", {_this call FUNC(KickPlayerBaseFired)}];

if (GVAR(no_3rd_person) == 0) then {
	execFSM "fsms\3rdperson.fsm";
};

GVAR(msg_hud_array) = [];

GVAR(last_hud_msgs) = [];
0 spawn FUNC(HudDispMsgEngine);

GVAR(msg_hud_shown) = false;

__ccppfln(x_client\x_marker.sqf);

__ccppfln(x_client\x_playerammobox.sqf);

GVAR(rscCrewTextShownTimeEnd) = -1;

(findDisplay 46) displayAddEventHandler ["MouseZChanged", {_this call FUNC(MouseWheelRec)}];

if (GVAR(WithRevive) == 0) then {
	__ccppfln(x_revive.sqf);
};

// GVAR(CurPIPVideoTarget) = objNull;

// FUNC(setVideoPipTarget) = {
	// if (GVAR(still_in_intro) || {!isNil "KEGs_SPECTATINGON"}) exitWith {};
	// private ["_poscam"];
	// GVAR(CurPIPVideoTarget) = __XJIPGetVar(GVAR(CurPIPVideoTarget));
	// if (!isNil QGVAR(PIP_screenCam)) then {
		// detach GVAR(PIP_screenCam);
	// };
	// _poscam = (vehicle GVAR(CurPIPVideoTarget)) modelToWorld [0, -5, 2];
	// if (isNil QGVAR(PIP_screenCam)) then {
		// GVAR(PIP_screenCam) = "camera" camCreate _poscam;
		// GVAR(PIP_screenCam) camPrepareFov 0.100;
		// GVAR(PIP_screenCam) cameraEffect ["INTERNAL", "BACK", QGVAR(rendertarget0)];
		// GVAR(video_wall1) setObjectTexture [0, "#(argb,256,256,1)r2t(d_rendertarget0,1.0)"];
		// QGVAR(rendertarget0) setPiPEffect [0];
	// } else {
		// detach GVAR(PIP_screenCam);
		// GVAR(PIP_screenCam) camSetPos _poscam;
	// };
	// GVAR(PIP_screenCam) camPrepareTarget (vehicle GVAR(CurPIPVideoTarget));
	// GVAR(PIP_screenCam) camCommitPrepared 0;
	// GVAR(PIP_screenCam) attachTo [vehicle GVAR(CurPIPVideoTarget), [0,-5,2]];
	// GVAR(pipt_isinvec) = vehicle GVAR(CurPIPVideoTarget) != GVAR(CurPIPVideoTarget);
// };

// 0 spawn {
	// scriptName "spawn_pip_checking";
	// waitUntil {sleep 0.312;!GVAR(still_in_intro)};
	// waitUntil {sleep 0.312; !isNil {__XJIPGetVar(GVAR(CurPIPVideoTarget))}};
	// waitUntil {sleep 0.312; !isNull __XJIPGetVar(GVAR(CurPIPVideoTarget))};
	// while {true} do {
		// waitUntil {sleep 0.123;isNil "KEGs_SPECTATINGON"};
		// if ((GVAR(pipt_isinvec) && {(vehicle GVAR(CurPIPVideoTarget) == GVAR(CurPIPVideoTarget))}) || {(!GVAR(pipt_isinvec) && {(vehicle GVAR(CurPIPVideoTarget) != GVAR(CurPIPVideoTarget))})) then {
			// GVAR(pipt_isinvec) = !GVAR(pipt_isinvec);
			// if (!isNil QGVAR(PIP_screenCam)) then {
				// detach GVAR(PIP_screenCam);
			// };
			// GVAR(PIP_screenCam) camPrepareTarget (vehicle GVAR(CurPIPVideoTarget));
			// GVAR(PIP_screenCam) camCommitPrepared 0;
			// GVAR(PIP_screenCam) attachTo [vehicle GVAR(CurPIPVideoTarget), [0,-5,2]];
		// };
		// sleep 1.211;
	// };
// };

// if (!isNil {__XJIPGetVar(GVAR(CurPIPVideoTarget))}) then {
	// 0 spawn {
		// scriptName "spawn_pipcheck2";
		// waitUntil {sleep 0.312;!GVAR(still_in_intro)};
		// if (!isNull __XJIPGetVar(GVAR(CurPIPVideoTarget))) then {
			// call FUNC(setVideoPipTarget);
		// };
	// };
// };

GVAR(commandingMenuIniting) = false;
// TYRGHEN - Added test
if (isNil "TYR_MENU_IsActive" || {TYR_MENU_IsActive <= 0}) then {
	["dom_command_menu", {
		if (!isNil QGVAR(commandingMenuCode)) then {
			if (!GV(player,xr_pluncon) && {alive player}) then {
				if (commandingMenu == "") then {
					call GVAR(commandingMenuCode);
					GVAR(commandingMenuCode) = nil;
					GVAR(DomCommandingMenuBlocked) = false;
				};
			} else {
				GVAR(commandingMenuCode) = nil;
				GVAR(DomCommandingMenuBlocked) = false;
				GVAR(commandingMenuIniting) = false;
			};
		};
	}, 0.1] call FUNC(addPerFrame);
};

GVAR(DomCommandingMenuBlocked) = false;

// TYRGHEN
(findDisplay 46) displayAddEventHandler ["KeyDown", {_this call FUNC(KeyDown_Handler)}];
(findDisplay 46) displayAddEventHandler ["KeyUp", {_this call FUNC(KeyUp_Handler)}];

// TYRGHEN - Adding the menu for the Domination options
[] call FUNC(createtyrusermenu);

if (isMultiplayer) then {
	execVM "x_client\x_respawnblock.sqf";
};

{
	GVAR(check_ammo_load_vecs) set [_forEachIndex, toUpper _x];
} forEach GVAR(check_ammo_load_vecs);

GVAR(playerInMHQ) = false;
GVAR(player_in_vec) = false;
[_pos, [0, 0, 0, false], ["NONE", "PRESENT", true], ["vehicle player != player && {alive player} && {!(player getVariable 'xr_pluncon')}", "call d_fnc_vehicleScripts","d_player_in_vec = false"]] call FUNC(CreateTrigger);

player setVariable ["d_p_isadmin", false];
GVAR(clientScriptsAr) = [false, false];
if (GVAR(AutoKickTime) == 0 || {GVAR(with_ranked)} || {GVAR(MissionType) == 2}) then {
	GVAR(clientScriptsAr) set [1, true];
};

[_pos, [0, 0, 0, false], ["NONE", "PRESENT", false], ["call d_fnc_startClientScripts;false", "", ""]] call FUNC(CreateTrigger);

_mrunwm = markerPos QGVAR(runwaymarker);
if (str _mrunwm != "[0,0,0]") then {
	_msize = markerSize QGVAR(runwaymarker);
	[_mrunwm, [_msize select 0, _msize select 1, markerDir QGVAR(runwaymarker), true], ["ANY", "PRESENT", true], ["count (thislist unitsBelowHeight 1) > 0", "'d_runwaymarker' setMarkerColorLocal 'ColorRed'", "'d_runwaymarker' setMarkerColorLocal 'ColorGreen'"]] call FUNC(CreateTrigger);
};

if (!isNil "HC_D_UNIT" && {!isNull HC_D_UNIT}) then {
	hideObject HC_D_UNIT;
};

{
	_x allowDamage false;
} forEach __XJIPGetVar(GVAR(bonus_air_buildings));

__ccppfln(scripts\inithcam.sqf);

// A3 has a stance indicator now
if (GVAR(UseDomStanceIndicator) == 0) then {
	execVM "x_client\x_stanceindicator.sqf";
};

player call FUNC(removefak);
player call FUNC(removetech);
// TYRGHEN
// The purpose of the following is to get the name of the leader of the player group.
// Say that the player is "alpha_2", his leader is thus "alpha_1". The Player will be able to teleport to him.
GVAR(player_leader_var) = "";
_player_char = toArray(str(player));
_group_char = [];
{
	_group_char set [count _group_char, _x];
	if (_x == 95) exitWith {};
} foreach _player_char;
if ((count _group_char > 1) && (count _group_char < count _player_char)) then {
	// We add a "1" at the end.
	_group_char set [count _group_char, 49];
	GVAR(player_leader_var) = toString _group_char;
};
	
if (!GVAR(with_ranked)) then {
	_pgearl = profileNamespace getVariable QGVAR(dom_gear_l);
	__TRACE_1("","_pgearl");
	if (!isNil "_pgearl") then {
		switch (str GVAR(player_side)) do {
			case "WEST": {
				_helper = (_pgearl select 0) select 1;
				__TRACE_1("","_helper");
				if (count _helper > 0) then {
					GVAR(custom_layout) = _helper;
				};
			};
			case "EAST": {
				_helper = (_pgearl select 1) select 1;
				if (count _helper > 0) then {
					GVAR(custom_layout) = _helper;
				};
			};
			case "RESISTANCE": {
				_helper = (_pgearl select 2) select 1;
				if (count _helper > 0) then {
					GVAR(custom_layout) = _helper;
				};
			};
			default {
				_helper = (_pgearl select 3) select 1;
				if (count _helper > 0) then {
					GVAR(custom_layout) = _helper;
				};
			};
		};
		call FUNC(retrieve_layoutgear);
	};
};

call FUNC(save_respawngear);

0 spawn FUNC(nostreaming);

0 spawn {
	scriptName "spawn_client_clean_crater";
	private "_craters";
	while {true} do {
		sleep (60 + random 60);
		_craters = allMissionObjects "CraterLong" + allMissionObjects "CraterLong_small";
		{
			deleteVehicle _x;
			sleep 0.212;
		} forEach _craters;
		_craters = nil;
	};
};

GVAR(areArtyVecsAvailable) = false;
GVAR(ao_arty_vecs) = [];
for "_i" from 0 to 20 do {
	_vvx = missionNamespace getVariable format ["d_arty%1_%2", (GVAR(player_can_call_arti) max 1), _i];
	if (!isNil "_vvx") then {
		GVAR(ao_arty_vecs) set [count GVAR(ao_arty_vecs), _vvx];
		GVAR(areArtyVecsAvailable) = true;
	};
};

__ccppfln(x_clientcustomcode.sqf);
 
if (isMultiplayer) then {execVM "x_client\x_intro.sqf"};

deleteVehicle GVAR(client_init_trig);
GVAR(client_init_trig) = nil;

diag_log [diag_frameno, diag_ticktime, time, "Dom x_setupplayer.sqf processed"];